package com.wed.db;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class InsertUserAssServlet
 */
@WebServlet("/InsertUserAssServlet")
public class InsertUserAssServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertUserAssServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String date=request.getParameter("DateGive");
		String user=request.getParameter("username");
		 int assi = Integer.parseInt(request.getParameter("AssignmentID"));

			String[] checkNameShare = (request.getParameterValues("classId"));
			List list = Arrays.asList(checkNameShare);
			request.setAttribute("check", list);
			List<String> listclassId = (List<String>) request
					.getAttribute("check");
     	
     	
			for (String lc : listclassId) 
			{
				ArrayList<User> listUser=new ConnectDB().getListStudentByClassID(lc);
			
				for(User us : listUser)
				{
				User_Assi ls=new User_Assi( lc,assi, date,us.getUserName());
				ConnectDB db=new ConnectDB();
				try
				{
				if(db.insertUserAssi(ls))
				{
					RequestDispatcher dispacher=request.getRequestDispatcher("CmapLecture.jsp");
			    	dispacher.forward(request, response);
				}
				else
				{response.sendRedirect("error.jsp");}

					
				
				}
				catch(Exception e)
				{
					 e.printStackTrace();
				}
				}
			}
	}

}
